/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserDialog.java
 *
 * Created on 25 Jan 2011, 11:57:34 AM
 */
package za.co.pas.business.view.dlg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import za.co.pas.business.data.Address;
import za.co.pas.business.data.Person;
import za.co.pas.business.data.UserLoginDetails;
import za.co.pas.business.data.UserType;
import za.co.pas.business.abstracts.AbstractDataModel;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.view.model.combo.UserTypeComboModel;

/**
 *
 * @author alabuschagne
 */
public class UserDialog extends JDialog
{

    private boolean ok = false;
    private UserLoginDetails userLoginDetails;
    private UserTypeComboModel userTypeComboModel = new UserTypeComboModel();
    private Address streetAddress;
    private Address postalAddress;
    private static final String DLG_TITLE = "User details: ";

    /** Creates new form UserDialog */
    public UserDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        setTitle(DLG_TITLE + "[NEW]");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        userUsernameTextField = new javax.swing.JTextField();
        userUserTypeComboBox = new javax.swing.JComboBox();
        userFirstnameTextField = new javax.swing.JTextField();
        userLastnameTextField = new javax.swing.JTextField();
        userTelTextField = new javax.swing.JTextField();
        userFaxTextField = new javax.swing.JTextField();
        userCelTextField = new javax.swing.JTextField();
        userEmailTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        userStreetAddressLabel = new javax.swing.JLabel();
        userPostalAddressLabel = new javax.swing.JLabel();
        userEnabledCheckBox = new javax.swing.JCheckBox();
        userPasswordTextField = new javax.swing.JPasswordField();
        userPassword2TextField = new javax.swing.JPasswordField();
        jPanel1 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("User name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel3, gridBagConstraints);

        jLabel4.setText("Confirm password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel4, gridBagConstraints);

        jLabel5.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel5, gridBagConstraints);

        jLabel6.setText("User type:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel6, gridBagConstraints);

        jLabel7.setText("First name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Last name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Tel:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel9, gridBagConstraints);

        jLabel10.setText("Fax:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel10, gridBagConstraints);

        jLabel11.setText("Cel:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel11, gridBagConstraints);

        jLabel12.setText("Email:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel12, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userUsernameTextField, gridBagConstraints);

        userUserTypeComboBox.setModel(userTypeComboModel);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userUserTypeComboBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userFirstnameTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userLastnameTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userTelTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userFaxTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userCelTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userEmailTextField, gridBagConstraints);

        jLabel15.setText("Street address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        jPanel4.add(jLabel15, gridBagConstraints);

        jLabel16.setText("Postal address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 10, 5);
        jPanel4.add(jLabel16, gridBagConstraints);

        jLabel17.setText("                                        ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 10);
        jPanel4.add(jLabel17, gridBagConstraints);

        userStreetAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        userStreetAddressLabel.setOpaque(true);
        userStreetAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userStreetAddressLabelMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userStreetAddressLabel, gridBagConstraints);

        userPostalAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        userPostalAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userPostalAddressLabelMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 10);
        jPanel4.add(userPostalAddressLabel, gridBagConstraints);

        userEnabledCheckBox.setText("Enabled");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 5);
        jPanel4.add(userEnabledCheckBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userPasswordTextField, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        jPanel4.add(userPassword2TextField, gridBagConstraints);

        jScrollPane1.setViewportView(jPanel4);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Action", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap(349, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean comparePasswords()
    {
        if (userPasswordTextField.getPassword().length != userPassword2TextField.getPassword().length)
        {
            return false;
        }
        for (int i = 0; i < this.userPasswordTextField.getPassword().length; i++)
        {
            if (userPasswordTextField.getPassword()[i] != userPassword2TextField.getPassword()[i])
            {
                return false;
            }
        }
        return true;
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed

        boolean newUser = false;
        //populate the fields
        if (userLoginDetails == null)
        {
            userLoginDetails = new UserLoginDetails();
            newUser = true;
        }
        if (newUser && (this.userPasswordTextField.getPassword().length == 0))
        {
            JOptionPane.showMessageDialog(this, "User needs a password", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (!comparePasswords())
        {
            JOptionPane.showMessageDialog(this, "Passwords not the same", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (this.userPasswordTextField.getPassword().length > 0)//then change it
        {
            userLoginDetails.setPassword(AbstractDataModel.doMD5(new String(userPasswordTextField.getPassword())));
        }
        userLoginDetails.setEnabled(this.userEnabledCheckBox.isSelected());
        userLoginDetails.setUserName(this.userUsernameTextField.getText());
        userLoginDetails.setUserType((UserType) this.userTypeComboModel.getSelectedItem());
        //contact details
        Person person = userLoginDetails.getContactPerson();
        if (person == null)
        {
            person = new Person();
            userLoginDetails.setContactPerson(person);
        }
        person.setCel(this.userCelTextField.getText());
        person.setEmail(this.userEmailTextField.getText());
        person.setFax(this.userFaxTextField.getText());
        person.setFirstName(this.userFirstnameTextField.getText());
        Address sAddress = person.getStreetAddress();
        if (sAddress == null)
        {
            sAddress = new Address();
            person.setStreetAddress(sAddress);
        }
        if (streetAddress != null)
        {
            sAddress.setCity(streetAddress.getCity());
            sAddress.setCode(streetAddress.getCode());
            sAddress.setCountry(streetAddress.getCountry());
            sAddress.setLine1(streetAddress.getLine1());
            sAddress.setLine2(streetAddress.getLine2());
            sAddress.setProvince(streetAddress.getProvince());
            sAddress.setSuburb(streetAddress.getSuburb());
        }
        else
        {
            sAddress.setCity("");
            sAddress.setCode("");
            sAddress.setCountry(null);
            sAddress.setLine1("");
            sAddress.setLine2("");
            sAddress.setProvince(null);
            sAddress.setSuburb("");
        }
        Address pAddress = person.getPostalAddress();
        if (pAddress == null)
        {
            pAddress = new Address();
            person.setPostalAddress(pAddress);
        }
        if (postalAddress != null)
        {
            pAddress.setCity(postalAddress.getCity());
            pAddress.setCode(postalAddress.getCode());
            pAddress.setCountry(postalAddress.getCountry());
            pAddress.setLine1(postalAddress.getLine1());
            pAddress.setLine2(postalAddress.getLine2());
            pAddress.setProvince(postalAddress.getProvince());
            pAddress.setSuburb(postalAddress.getSuburb());
        }
        else
        {
            pAddress.setCity("");
            pAddress.setCode("");
            pAddress.setCountry(null);
            pAddress.setLine1("");
            pAddress.setLine2("");
            pAddress.setProvince(null);
            pAddress.setSuburb("");
        }
        person.setSurName(this.userLastnameTextField.getText());
        person.setTel(this.userTelTextField.getText());
        setOk(true);
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
        setOk(false);
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void userStreetAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_userStreetAddressLabelMouseReleased
    {//GEN-HEADEREND:event_userStreetAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editStreetAddress();
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_userStreetAddressLabelMouseReleased

    private void userPostalAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_userPostalAddressLabelMouseReleased
    {//GEN-HEADEREND:event_userPostalAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editPostalAddress();
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_userPostalAddressLabelMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField userCelTextField;
    private javax.swing.JTextField userEmailTextField;
    private javax.swing.JCheckBox userEnabledCheckBox;
    private javax.swing.JTextField userFaxTextField;
    private javax.swing.JTextField userFirstnameTextField;
    private javax.swing.JTextField userLastnameTextField;
    private javax.swing.JPasswordField userPassword2TextField;
    private javax.swing.JPasswordField userPasswordTextField;
    private javax.swing.JLabel userPostalAddressLabel;
    private javax.swing.JLabel userStreetAddressLabel;
    private javax.swing.JTextField userTelTextField;
    private javax.swing.JComboBox userUserTypeComboBox;
    private javax.swing.JTextField userUsernameTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the ok
     */
    public boolean isOk()
    {
        return ok;
    }

    /**
     * @param ok the ok to set
     */
    public void setOk(boolean ok)
    {
        this.ok = ok;
    }

    /**
     * @return the userLoginDetails
     */
    public UserLoginDetails getUserLoginDetails()
    {
        return userLoginDetails;
    }

    private void updateData()
    {
        if (userLoginDetails != null)
        {
            userUsernameTextField.setText(this.userLoginDetails.getUserName());
            userUserTypeComboBox.setSelectedItem(this.userLoginDetails.getUserType());
            userPasswordTextField.setText("");
            userPassword2TextField.setText("");
            userEnabledCheckBox.setSelected(this.userLoginDetails.getEnabled());
            if (this.userLoginDetails.getContactPerson() != null)
            {
                userCelTextField.setText(this.userLoginDetails.getContactPerson().getCel());
                userEmailTextField.setText(this.userLoginDetails.getContactPerson().getEmail());
                userFaxTextField.setText(this.userLoginDetails.getContactPerson().getFax());
                userFirstnameTextField.setText(this.userLoginDetails.getContactPerson().getFirstName());
                userLastnameTextField.setText(this.userLoginDetails.getContactPerson().getSurName());
                userTelTextField.setText(this.userLoginDetails.getContactPerson().getTel());
                streetAddress = this.userLoginDetails.getContactPerson().getStreetAddress();
                if (streetAddress != null)
                {
                    userStreetAddressLabel.setText(streetAddress.toString());
                }
                else
                {
                    userStreetAddressLabel.setText("");
                }
                postalAddress = this.userLoginDetails.getContactPerson().getPostalAddress();
                if (postalAddress != null)
                {
                    userPostalAddressLabel.setText(postalAddress.toString());
                }
                else
                {
                    userPostalAddressLabel.setText("");
                }
            }
            else
            {
                userCelTextField.setText("");
                userEmailTextField.setText("");
                userFaxTextField.setText("");
                userFirstnameTextField.setText("");
                userLastnameTextField.setText("");
                userTelTextField.setText("");
                userStreetAddressLabel.setText("");
            }
            setTitle(DLG_TITLE + userLoginDetails.getUserName());
        }
        else
        {
            userEnabledCheckBox.setSelected(true);
            userUsernameTextField.setText("");
            userUserTypeComboBox.setSelectedIndex(-1);
            userPasswordTextField.setText("");
            userPassword2TextField.setText("");
            userCelTextField.setText("");
            userEmailTextField.setText("");
            userFaxTextField.setText("");
            userFirstnameTextField.setText("");
            userLastnameTextField.setText("");
            userTelTextField.setText("");
            userStreetAddressLabel.setText("");
            setTitle(DLG_TITLE + "[NEW]");
        }
    }

    /**
     * @param userLoginDetails the userLoginDetails to set
     */
    public void setUserLoginDetails(UserLoginDetails userLoginDetails)
    {
        if (userLoginDetails != null)
        {
            this.userLoginDetails = userLoginDetails.clone();
        }
        updateData();
    }

    private void editStreetAddress()
    {
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Street Address", "Street 1:", "Street 2:");
        if (userLoginDetails != null)
        {
            if (this.userLoginDetails.getContactPerson() != null)
            {
                ad.setAddress(userLoginDetails.getContactPerson().getStreetAddress());
            }
        }
        ad.setVisible(true);
        if (ad.isOk())
        {
            if (userLoginDetails == null)
            {
                userLoginDetails = new UserLoginDetails();
            }
            Person p = userLoginDetails.getContactPerson();
            if (p == null)
            {
                p = new Person();
                userLoginDetails.setContactPerson(p);
            }
            Address s = p.getStreetAddress();
            if (s == null)
            {
                s = new Address();
                p.setStreetAddress(s);
            }
            s.setCity(ad.getAddressPanel().getCity());
            s.setCode(ad.getAddressPanel().getCode());
            s.setCountry(ad.getAddressPanel().getCountry());
            s.setLine1(ad.getAddressPanel().getStreet1());
            s.setLine2(ad.getAddressPanel().getStreet2());
            s.setProvince(ad.getAddressPanel().getProvince());
            s.setSuburb(ad.getAddressPanel().getSuburb());
        }
        updateData();
    }

    private void editPostalAddress()
    {
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Postal Address", "Postal 1:", "Postal 2:");
        if (userLoginDetails != null)
        {
            if (this.userLoginDetails.getContactPerson() != null)
            {
                ad.setAddress(userLoginDetails.getContactPerson().getPostalAddress());
            }
        }
        ad.setVisible(true);
        if (ad.isOk())
        {
            if (userLoginDetails == null)
            {
                userLoginDetails = new UserLoginDetails();
            }
            Person p = userLoginDetails.getContactPerson();
            if (p == null)
            {
                p = new Person();
                userLoginDetails.setContactPerson(p);
            }
            Address s = p.getPostalAddress();
            if (s == null)
            {
                s = new Address();
                p.setPostalAddress(s);
            }
            s.setCity(ad.getAddressPanel().getCity());
            s.setCode(ad.getAddressPanel().getCode());
            s.setCountry(ad.getAddressPanel().getCountry());
            s.setLine1(ad.getAddressPanel().getStreet1());
            s.setLine2(ad.getAddressPanel().getStreet2());
            s.setProvince(ad.getAddressPanel().getProvince());
            s.setSuburb(ad.getAddressPanel().getSuburb());
        }
        updateData();
    }
}
